<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/10/12
 * Time: 10:13
 */

namespace App\admin\TableView;


use App\admin\TableView\BaseClass\BaseViewList;
use App\Jobs\Fun;
use App\Models\Admin\ApilistModel;
use App\Models\Admin\CategoryModel;
use App\Models\Admin\CompanyModel;
use App\Models\Admin\UserModel;
use App\Models\AreaModel;
use App\Models\User\UsergongchangModel;
use App\Models\User\UserGongsiModel;
use App\Models\User\UserOptionsGongshiModel;
use App\Services\Jiguang\JMessageApi;
use Faker\Provider\Company;

class UserShenheGongchangView extends BaseViewList
{

    public function get_model()
    {

        return new UsergongchangModel();
    }

    public $show_add = false; ///显示添加按钮
    /**
     * 是否显示删除按钮
     */
    public $show_delete = false;
    public $topTitle = '工厂资料审核';
    public $PageSize = 10;

    public $breadcrumb = '<li><a href="/mcadmin">Home</a></li>
        <li><a href="javascript:;">资料审核</a></li>
        ';

    public $html = '';


    //  public $show_add = false; ///显示添加按钮

    /**
     * 获取表格的唯一名字
     */
    public function getName()
    {
        return 'CompanyModel';
    }


    /**
     * 需要继承,然后定义每行需要显示的数据格式
     */
    public function Labels()
    {
        return array(
            //   BaseViewList::Button_checkbox=>[],
            'id' => 40,
            'uid' => 50,

            'facType' => 50,
            'legal_name' => 50,
            'legal_sex' => 40,
            'legal_mobile' => 100,
            'shenhe' => 40,
            //  'visible' => 40,
            'com_city' => 140,
            'com_address' => 140,
            BaseViewList::Button_control => [

                BaseViewList::Button_Edit,
                //   BaseViewList::Button_Delete

            ]
        );
    }

    /**
     * 对话框中的各个字段
     */
    public function dialog()
    {
        return array(

            array(
                'zd' => 'shenhe',
                'type' => self::dialog_list,
                'data' => CompanyModel::toShenhe()
            )
        );
    }

    public function save_model($model){
        $old=UsergongchangModel::where('id',$model->id)->select([
            'shenhe'
        ])->first();
        $oldshenhe=$old->shenhe;
        $newShenhe=$model->shenhe;
       // $type=$model['type'];
        if($oldshenhe==CompanyModel::shenhe_yes&&$newShenhe!=CompanyModel::shenhe_yes){
            ///如果旧的数据是审核通过并且新的不是审核通过

             $userinfo=\App\Models\User\UserModel::getinfo($model->uid);
           if($userinfo['is_factory']==\App\Models\User\UserModel::is_kaiguan_yes){
                $this->errorMsg='工厂开关是打开的状态,无法设置为非通过状态;请先关闭该用户的工厂开关';
                return false;
            }
        }
        if($oldshenhe!=CompanyModel::shenhe_yes&&$newShenhe==CompanyModel::shenhe_yes){

                $this->isSendMsg=true;
                $this->SendMsg='您填写的工厂信息已经通过了审核,您可以打开工厂开关';


        }
        if($oldshenhe!=CompanyModel::shenhe_jujue&&$newShenhe==CompanyModel::shenhe_jujue){

                $this->isSendMsg=true;
                $this->SendMsg='您填写的工厂信息没有通过审核';

        }



        return true;
    }


    /**
     * 是否需要发送消息
     */
    private $isSendMsg=false;
    /**
     * 需要发送的消息
    */
    private $SendMsg='';

    /**
     * 保存成功后调用
     */
    public function save_success($model,$isNewRecord) {
        if($this->isSendMsg){
            JMessageApi::SendMsg($model->uid,$this->SendMsg);
        }
        return true;
    }






    /**
     * 方便继承,多一层过滤
     */
    public function ModelTohtml($key, $model)
    {
         if($key=='shenhe'){
            return CompanyModel::toShenhe($model->$key);
        }
        if($key=='facType'){
            return CategoryModel::getName($model->facType);
        }
        if($key=='com_city'){
            return AreaModel::getMergerName($model->$key);
        }
        if($key=='legal_sex'){
            return \App\Models\User\UserModel::sexTo($model->$key);
        }

        return parent::ModelTohtml($key, $model);
    }

    /**
     * 返回查询的条件
     */
    public function find()
    {

         $find = $this->get_model()->where([

            ]);


        $find->orderBy($this->get_primaryKey(), 'DESC');
        return $find;
    }

}
